package com.spicdt.edge.protocol.door.demo.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.spicdt.edge.protocol.door.demo.conf.DoorConf;
import com.spicdt.edge.protocol.door.demo.model.dto.*;
import com.spicdt.edge.protocol.door.demo.model.po.Department;
import com.spicdt.edge.protocol.door.demo.utils.UrlConstants;
import com.spicdt.iedge.psdk.data.Response;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.util.*;

@Slf4j
public class DoorBaseServiceImpl extends DoorApiBase implements DoorBaseService {


    public DoorBaseServiceImpl(DoorConf config) {
        super(config);

    }

    /**
     * 检查鑫业平台心跳状态
     *
     * @return 响应结果（code=200 表示成功）
     */
    @Override
    public Response heartbeat() {
        Map<String, Object> paramMap = Map.of("flag", 1);

        log.info("开始检测鑫业平台心跳状态，flag={}", 1);

        try {
            return doPostJsonResultBase(UrlConstants.URL_HEARTBEAT, paramMap);
        } catch (Exception e) {
            log.error("心跳检测失败：{}", e.getMessage(), e);
            return Response.fail("HEARTBEAT_ERROR", "心跳检测异常：" + e.getMessage(), null);
        }
    }

    @Override
    public Response getToken(DoorConf doorConf) {
        Response response = doPostJsonResultBase(UrlConstants.URL_XY_GET_TOKEN, JSONUtil.parseObj(doorConf));
        if (response.isSuccess() && null != response.getData()) {
            return response;
        } else {

            return Response.fail(response.getCode(), "获取Token失败{}", response.getData());
        }
    }


    /**
     * 完成
     *
     * @return
     */
    @Override
    public Response allDoorState(DeviceRecordQueryDTO deviceRecordQueryDTO) {
        Map<String, Object> paramMap = BeanUtil.beanToMap(deviceRecordQueryDTO);
        return doPostJsonResultBase(UrlConstants.URL_XY_GET_DEVICE_STATUS, paramMap);
    }


    @Override
    public Response getDoorEventList(String startTime, String endTime, Integer pageNo, Integer pageSize) {
        Map paramMap = new HashMap();
//        paramMap.put("controllerID",001004);
//        paramMap.put("doorID",1);
        paramMap.put("startDateTime", startTime);
        paramMap.put("endDateTime", endTime);
        paramMap.put("queryPage", pageNo);
        paramMap.put("lineSize", pageSize);
        paramMap.put("order", 2);
        Response response = doPostJsonResultBase(UrlConstants.URL_XY_GET_DEVICE_RECORD, paramMap);
        return response;
    }

    @Override
    public Response getStaffInfo(UserQueryDTO userQueryDTO) {
        Map<String, Object> paramMap = new HashMap<>();

        BeanUtil.beanToMap(userQueryDTO, paramMap, false, null);
        Response response = doPostJsonResultBase(UrlConstants.URL_STAFF_GET_STAFF, paramMap);
        return response;
    }

    @Override
    public Response addStaffInfo(StaffInfoDTO staffInfoDTO) {
        ObjectMapper objectMapper = new ObjectMapper();
        Map paramMap = objectMapper.convertValue(staffInfoDTO, Map.class);
        Response response = doPostJsonResultBase(UrlConstants.URL_STAFF_ADD_STAFF, paramMap);
        return response;
    }

    @Override
    public Response deleteDoorPermission(DeleteAccessByDoorsDTO deleteAccessByDoorsDTO) {
        ObjectMapper objectMapper = new ObjectMapper();
        Map paramMap = objectMapper.convertValue(deleteAccessByDoorsDTO, Map.class);
        Response response = doPostJsonResultBase(UrlConstants.URL_ACCESS_DELETE_ACCESS_BY_DOORS, paramMap);
        return response;
    }

    @Override
    public Response editDoorPermission(EditAccessByDoorsDTO editAccessByDoorsDTO) {
        ObjectMapper objectMapper = new ObjectMapper();
        Map paramMap = objectMapper.convertValue(editAccessByDoorsDTO, Map.class);
        Response response = doPostJsonResultBase(UrlConstants.URL_ACCESS_EDIT_ACCESS_BY_DOORS, paramMap);
        return response;
    }

    @Override
    public Response getDepartmentInfo(DepartmentQueryDTO departmentQueryDTO) {
        ObjectMapper objectMapper = new ObjectMapper();
        Map paramMap = objectMapper.convertValue(departmentQueryDTO, Map.class);
        Response response = doPostJsonResultBase(UrlConstants.URL_DEPARTMENT_GET_DEPARTMENT, paramMap);
        return response;
    }

    @Override
    public Response updateDepartmentInfo(Department department) {
        ObjectMapper objectMapper = new ObjectMapper();
        Map paramMap = objectMapper.convertValue(department, Map.class);
        Response response = doPostJsonResultBase(UrlConstants.URL_DEPARTMENT_UPDATE_DEPARTMENT, paramMap);
        return response;
    }

    @Override
    public Response deleteDepartmentInfo(String departmentID) {
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("departmentID", departmentID);
        Response response = doPostJsonResultBase(UrlConstants.URL_DEPARTMENT_DELETE_DEPARTMENT, paramMap);
        return response;
    }

    @Override
    public Response addDepartmentInfo(AddDepartmentDTO addDepartmentDTO) {
        ObjectMapper objectMapper = new ObjectMapper();
        Map paramMap = objectMapper.convertValue(addDepartmentDTO, Map.class);
        Response response = doPostJsonResultBase(UrlConstants.URL_DEPARTMENT_ADD_DEPARTMENT, paramMap);
        return response;
    }

    @Override
    public Response remoteControl(DeviceRemoteControlDTO deviceRemoteControlDTO) {
        ObjectMapper objectMapper = new ObjectMapper();
        Map paramMap = objectMapper.convertValue(deviceRemoteControlDTO, Map.class);
        Response response = doPostJsonResultBase(UrlConstants.URL_DEVICE_REMOTE_CONTROL, paramMap);
        return response;
    }

    @Override
    public Response getDeviceRecord(DeviceRecordQueryDTO deviceRecordQueryDTO) {
        ObjectMapper objectMapper = new ObjectMapper();
        Map paramMap = objectMapper.convertValue(deviceRecordQueryDTO, Map.class);
        Response response = doPostJsonResultBase(UrlConstants.URL_RECORD_STATUS_GET_DEVICE_RECORD, paramMap);
        return response;
    }

    @Override
    public Response getEventCode() {
        Response response = doPostJsonResultBase(UrlConstants.URL_RECORD_STATUS_GET_EVENT_CODE, null);
        return response;
    }

    @Override
    public Response getLastRecord(StaffQueryDTO staffQueryDTO) {
        ObjectMapper objectMapper = new ObjectMapper();
        Map paramMap = objectMapper.convertValue(staffQueryDTO, Map.class);
        Response response = doPostJsonResultBase(UrlConstants.URL_RECORD_STATUS_GET_LAST_RECORD, paramMap);
        return response;
    }

    @Override
    public Response addCard(StaffCardDTO staffCardDTO) {
        ObjectMapper objectMapper = new ObjectMapper();
        Map paramMap = objectMapper.convertValue(staffCardDTO, Map.class);
        Response response = doPostJsonResultBase(UrlConstants.URL_STAFF_ADD_CARD, paramMap);
        return response;
    }

    @Override
    public Response deleteCard(String staffID, String cardID) {
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("staffID", staffID);
        paramMap.put("cardID", cardID);
        Response response = doPostJsonResultBase(UrlConstants.URL_STAFF_DELETE_CARD, paramMap);
        return response;
    }

    @Override
    public Response getCardType() {
        Response response = doPostJsonResultBase(UrlConstants.URL_STAFF_GET_CARD_TYPE, null);
        return response;
    }

    @Override
    public Response resignStaff(StaffResignDTO staffResignDTO) {
        ObjectMapper objectMapper = new ObjectMapper();
        Map paramMap = objectMapper.convertValue(staffResignDTO, Map.class);
        Response response = doPostJsonResultBase(UrlConstants.URL_STAFF_RESIGN_STAFF, paramMap);
        return response;
    }

    @Override
    public Response updateStaff(StaffInfoDTO staffInfoDTO) {
        ObjectMapper objectMapper = new ObjectMapper();
        Map paramMap = objectMapper.convertValue(staffInfoDTO, Map.class);
        Response response = doPostJsonResultBase(UrlConstants.URL_STAFF_UPDATE_STAFF, paramMap);
        return response;
    }

    @Override
    public void setCurrentToken(String currentToken) {
        log.info("开始设置当前Token: {}", currentToken);
        try {
            super.config.setCurrentToken(currentToken);
            log.info("成功设置当前Token: {}", currentToken);
        } catch (Exception e) {
            log.error("设置当前Token失败: {}, 异常信息: {}", currentToken, e.getMessage(), e);
            throw e;
        }
    }

}
